package com.kuaimacode.kframework.admin.controller.invite;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.coupon.CouponInfoService;
import com.kuaimacode.kframework.admin.services.invite.InviteService;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.mapper.invite.InviteMapper;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;
import com.kuaimacode.kframework.mybatis.models.coupon.CouponInfo;
import com.kuaimacode.kframework.mybatis.models.invite.Invite;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author anzy
 * @date 2019/03/31
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/invite"})
public class InviteController {
	private static String PAGE_FILE_NAME = "invite/";

	@Autowired
	private InviteService inviteServices;

	@Autowired
	private CouponInfoService couponInfoService;


	@Value("${invite.normal.url}")
	private String url;
	@Value("${invite.miniapp.url}")
	private String miniapp_url ;

	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<Invite> pageInfo = inviteServices.findAll(pageNum, pageSize);
		// pageInfo.list invite
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		Invite invite = new Invite();
		map.put("invite", invite);
		map.put("isAdd", true);
		List<CouponInfo> coupons = couponInfoService.queryNormal();
		map.put("coupons",coupons);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		Invite invite = inviteServices.getById(id);
		map.put("invite", invite);
		map.put("isAdd", false);
		List<CouponInfo> coupons = couponInfoService.queryNormal();
		map.put("coupons",coupons);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public JsonResult edit(Invite invite, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				// 新建保存
				invite.setCreateAt(new Date());
				invite.setCreateBy(SessionUtil.getUserId());
				inviteServices.save(invite);
				//更新url
                invite.setUrl(String.format(url,invite.getId()));
                invite.setMiniappUrl(String.format(miniapp_url,invite.getId()));
                inviteServices.updateById(invite);

			}else{
				// 编辑保存
				inviteServices.updateById(invite);
			}
			//如果该活动为立即生效，则更新其他活动为不生效，同一时间 只能有一个活动生效。
			if(Constant.IsNotEnum.YES.getCode()== invite.getIsAvailable()){
				inviteServices.updateIsAvailable(invite.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		Invite invite = inviteServices.getById(id);
		map.put("invite", invite);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	@RequestMapping(value="delete/{id}", method = RequestMethod.POST)
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			inviteServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
